package com.sixdog.regulate;

import com.sixdog.regulate.adapter.ActionAdapter;
import com.sixdog.regulate.bean.Action;
import com.sixdog.regulate.bean.Activity;
import com.sixdog.regulate.bean.Task;
import com.sixdog.regulate.deviceConfig.DeviceRefresh;
import com.sixdog.regulate.scheduled.EctActionScheduled;
import com.sixdog.regulate.service.ActivityService;
import com.sixdog.regulate.service.DeviceService;
import com.sixdog.regulate.service.GestureService;
import com.sixdog.regulate.service.KeycodeService;
import com.sixdog.regulate.service.impl.DeviceServiceImpl;
import com.sixdog.regulate.utils.TimeUtils;
import com.sixdog.regulate.windows.RegulateMain;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.stereotype.Component;


import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@SpringBootApplication
@EnableScheduling
@Component
public class Main implements ApplicationRunner {

    @Autowired
    private DeviceRefresh deviceRefresh;
    @Autowired
    private EctActionScheduled ectActionScheduled;


    @Autowired
    private RegulateMain regulateMain;

    public static void main(String[] args) {


        // 初始化一些参数......
        initSomething();
        SpringApplication.run(Main.class, args);
    }
//    gestureService.onClickCustom(deviceSerialno, 899, 2094, 2000, 50);
    // TODO：初始化Tasks
    private static void initSomething(){
        DeviceService deviceService = new DeviceServiceImpl();
        ArrayList<String> deviceSerials = deviceService.getDeviceSerials();
        Activity activity = new Activity();
        activity.setActivityName("com.taobao.taobao/com.taobao.android.purchase.aura.TBBuyActivity");
        activity.setDat("https://h5.m.taobao.com/cart/order.html?buyParam=671127842639_2");
        Date date = TimeUtils.getTime(2022,3,25,11,4,0);

        String cron = TimeUtils.getCron(date,20);
//      String cron = "40 58 16 22 3 *";
        long advanceMillisecond = 0;

        String dateServiceName = "taobao";

        ArrayList<Action> actions = new ArrayList<>();

        deviceSerials.forEach(deviceSerial -> {
            System.out.println(deviceSerial);
            Action action = new Action();
            action.setDate(date);
            ArrayList<String> functionNames = new ArrayList<>();
            String funName0 = "openActivityHighAuthority";
            Object[] args0 = {deviceSerial,activity};
            String funName1 = "onClickCustom";
            Object[] args1 = {deviceSerial, 899, 2094, 2000, 50};
            functionNames.add(funName0);
            functionNames.add(funName1);
            action.setFunctionNames(functionNames);
            Map<String, Object[]> argsMap = new HashMap<>();
            argsMap.put(funName0,args0);
            argsMap.put(funName1,args1);
            action.setArgsMap(argsMap);
            action.setCron(cron);
            action.setAdvanceMillisecond(advanceMillisecond);
            action.setDateServerName(dateServiceName);
            actions.add(action);
        });

        Task task = new Task();
        task.setActions(actions);
        task.setCron(cron);
        task.setDate(date);
        task.setEnable(true);
        ArrayList<Task> tasks = new ArrayList<>();
        tasks.add(task);

        // 为EctActionScheduled中的staticTasks赋值
        EctActionScheduled.setStaticTask(tasks);
    }

    /*
    * 单元测试
    * */

    @Autowired
    private DeviceService deviceService;
    @Autowired
    private ActivityService activityService;
    @Autowired
    private GestureService gestureService;
    @Autowired
    private KeycodeService keycodeService;
    @Autowired
    private ActionAdapter actionAdapter;

    @Override
    public void run(ApplicationArguments args) throws Exception {
        // 先初始化 再开启线程 否则 会出现NullPointerException
        regulateMain.init();
        new Thread(deviceRefresh).start();

    }

}
